package cn.swufe.edu.stu.hw_tools.Cal;

import static cn.swufe.edu.stu.hw_tools.StringToNumDec.calculateDec;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import cn.swufe.edu.stu.hw_tools.Convert.Dis_CV_Activity;
import cn.swufe.edu.stu.hw_tools.R;

public class CalculatorActivity extends AppCompatActivity {
    boolean canPoint = false;
    boolean canPoint2 = true;//为退格后可以加小数点考虑
    boolean hasOperater = true;
    int backcount = 0;//用于提示用户，退格机制可能不太完善
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
    }

    public void back(View btn){
        Intent intent = getIntent();
        setResult(2,intent);
        finish();
    }

    public void inputnums(View btn) {
        TextView textView = findViewById(R.id.shownums);
        TextView v = findViewById(R.id.showans);
        String str = textView.getText().toString();
        String add = getResources().getResourceEntryName(btn.getId());
        boolean hasCal = false;
        if (add.equals("one")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "1";
            textView.setText(str);
            if (hasOperater) {canPoint = true;}
        } else if (add.equals("two")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "2";
            textView.setText(str);
            if (hasOperater) {canPoint = true;}
        } else if (add.equals("three")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "3";
            textView.setText(str);
            if (hasOperater) {canPoint = true;}
        } else if (add.equals("four")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "4";
            textView.setText(str);
            if (hasOperater) {canPoint = true;}
        } else if (add.equals("five")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "5";
            if (hasOperater) {canPoint = true;}
            textView.setText(str);
        } else if (add.equals("six")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "6";
            if (hasOperater) {canPoint = true;}
            textView.setText(str);
        } else if (add.equals("seven")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "7";
            if (hasOperater) {canPoint = true;}
            textView.setText(str);
        } else if (add.equals("eight")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "8";
            if (hasOperater) {canPoint = true;}
            textView.setText(str);
        } else if (add.equals("nine")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "9";
            if (hasOperater) {canPoint = true;}
            textView.setText(str);
        } else if (add.equals("zero")) {
            if (str.equals("0")) {
                str = "";
            }
            str += "0";
            if (hasOperater) {canPoint = true;}
            textView.setText(str);
        } else if (add.equals("plus")) {
            str += "+";
            textView.setText(str);
            if(canPoint){canPoint2 = true;}
            canPoint = false;
            hasOperater = true;
        } else if (add.equals("chu")) {
            str += "/";
            textView.setText(str);
            if(canPoint){canPoint2 = true;}
            canPoint = false;
            hasOperater = true;
        } else if (add.equals("point")) {
            if (canPoint) {
                str += ".";
                textView.setText(str);
                hasOperater = false;
                canPoint = false;
            }
        } else if (add.equals("minus")) {
            str += "-";
            textView.setText(str);
            if(canPoint){canPoint2 = true;}
            canPoint = false;
            hasOperater = true;
        } else if (add.equals("chen")) {
            str += "x";
            textView.setText(str);
            if(canPoint){canPoint2 = true;}
            canPoint = false;
            hasOperater = true;
        } else if (add.equals("equal")) {
            try {
                double ans = calculateDec(str);
                String ans_str = String.valueOf(ans);
                v.setText(ans_str);
                backcount = 0;
            } catch (Exception e) {
                textView.setText("");
                v.setText("");
                Toast.makeText(this, "(#｀ε´# )ゞ请输入合法的表达式", Toast.LENGTH_SHORT).show();
            }
            canPoint = true;
            hasCal = true;
        } else if (add.equals("wipe")) {
            v.setText("0");
            textView.setText("0");
            canPoint = true;
            backcount = 0;
        } else if (add.equals("TGback")) {
            if(backcount > 5){
                Toast.makeText(this, "退格和小数点机制可能有bug(´ω｀*)...多次退格后，建议清零...", Toast.LENGTH_SHORT).show();
            }
            try {
                char lastChar = str.charAt(str.length() - 1);
                String resultString = str.substring(0, str.length() - 1);
                if(lastChar == '.'){canPoint = true;}
                else if(lastChar == '+' || lastChar == '-' || lastChar == 'x' || lastChar == '/')
                {if(canPoint2) canPoint = true;}
                textView.setText(resultString);
                backcount ++;
            } catch (Exception e) {
                Toast.makeText(this, "(#ﾟﾛﾟ#)！已经空了，别点了......", Toast.LENGTH_SHORT).show();
            }
        }else if (add.equals("zanwu") || add.equals("percent")) {
            Toast.makeText(this, "(´ω｀*)该按件暂不可用......", Toast.LENGTH_SHORT).show();
        }

        if (hasCal) {
            textView.setText("");
        }
    }
}
